#include "time_display.h"
#include <time.h>
#include <stdio.h>
// 添加必要的头文件引用
#include "../UI/ui.h"               // 引入ui_desktop的定义

// 声明所有字体变量（根据实际字体文件补充）
extern const lv_font_t ui_font_Font1;    // 如果有其他字体也一起声明
extern const lv_font_t ui_font_Font30;

// 全局控件指针（与UI代码风格一致）
lv_obj_t * ui_label_date = NULL;       // 日期和星期标签
lv_obj_t * ui_label_time = NULL;       // 时间标签
lv_obj_t * ui_label_duration = NULL;   // 时长标签
lv_timer_t * ui_time_update_timer = NULL;  // 定时器指针
static time_t start_time = 0;          // 计时基准时间

/**
 * 时间更新回调函数
 */
static void update_time_cb(lv_timer_t *timer) {
    time_t now;
    struct tm tm;

    // 获取当前本地时间
    now = time(NULL);
    localtime_r(&now, &tm);

    // 初始化开始时间
    if (start_time == 0) {
        start_time = now;
    }

    char time_str[9];         // 格式: HH:MM:SS
    char date_str[50];        // 格式: YYYY/MM/DD Weekday
    int total_minute = (now - start_time) / 60;  // 累计时长（分钟）

    // 星期字符串数组（与tm_wday对应：0=周日，1=周一...6=周六）
    const char *weekday_strs[] = {
        "Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday"
    };
    int weekday = tm.tm_wday % 7;  // 确保索引有效

    // 格式化时间和日期字符串
    snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d", 
             tm.tm_hour, tm.tm_min, tm.tm_sec);
    snprintf(date_str, sizeof(date_str), "%04d/%02d/%02d %s", 
             tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 
             weekday_strs[weekday]);

    // 更新标签文本
    lv_label_set_text(ui_label_time, time_str);
    lv_label_set_text(ui_label_date, date_str);
    lv_label_set_text_fmt(ui_label_duration, "Duration: %d min", total_minute);
}

/**
 * 初始化时间显示控件（与UI初始化风格一致）
 */
void ui_time_screen_init(void) {
    // 确保桌面UI已经初始化
    if(ui_desktop == NULL) {
        return; // 或者根据需要处理错误
    }

    // 创建日期标签（右上角顶部）
    ui_label_date = lv_label_create(ui_desktop);  // 父容器为桌面主界面
    lv_obj_set_width(ui_label_date, LV_SIZE_CONTENT);
    lv_obj_set_height(ui_label_date, LV_SIZE_CONTENT);
    lv_obj_set_x(ui_label_date, 360);  // 比之前小，左移
    lv_obj_set_y(ui_label_date, -260); //比之前大（更靠近屏幕中间），下移
    lv_obj_set_align(ui_label_date, LV_ALIGN_CENTER);
    lv_label_set_text(ui_label_date, "2023/01/01 Sunday");
    // 设置文本样式（与现有UI一致）
    lv_obj_set_style_text_color(ui_label_date, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_label_date, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_label_date, &ui_font_Font30, LV_PART_MAIN | LV_STATE_DEFAULT);  // 使用现有字体
    lv_obj_set_style_text_align(ui_label_date, LV_TEXT_ALIGN_RIGHT, LV_PART_MAIN | LV_STATE_DEFAULT);

    // 创建时间标签（日期标签下方）
    ui_label_time = lv_label_create(ui_desktop);
    lv_obj_set_width(ui_label_time, LV_SIZE_CONTENT);
    lv_obj_set_height(ui_label_time, LV_SIZE_CONTENT);
    lv_obj_set_x(ui_label_time, 440);
    lv_obj_set_y(ui_label_time, -200);
    lv_obj_set_align(ui_label_time, LV_ALIGN_CENTER);
    lv_label_set_text(ui_label_time, "00:00:00");
    // 设置文本样式
    lv_obj_set_style_text_color(ui_label_time, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_label_time, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_label_time, &ui_font_Font30, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_label_time, LV_TEXT_ALIGN_RIGHT, LV_PART_MAIN | LV_STATE_DEFAULT);

    // 创建时长标签（时间标签下方）
    ui_label_duration = lv_label_create(ui_desktop);
    lv_obj_set_width(ui_label_duration, LV_SIZE_CONTENT);
    lv_obj_set_height(ui_label_duration, LV_SIZE_CONTENT);
    lv_obj_set_x(ui_label_duration, 389);      // 比之前小，左移
    lv_obj_set_y(ui_label_duration, -230);
    lv_obj_set_align(ui_label_duration, LV_ALIGN_CENTER);
    lv_label_set_text_fmt(ui_label_duration, "Duration: %d min", 0);
    // 设置文本样式
    lv_obj_set_style_text_color(ui_label_duration, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_label_duration, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_label_duration, &ui_font_Font30, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui_label_duration, LV_TEXT_ALIGN_RIGHT, LV_PART_MAIN | LV_STATE_DEFAULT);

    // 创建更新定时器
    ui_time_update_timer = lv_timer_create(update_time_cb, 1000, NULL);
}

/**
 * 销毁时间显示控件（与UI销毁风格一致）
 */
void ui_time_screen_destroy(void) {
    // 删除标签控件
    if(ui_label_date) lv_obj_del(ui_label_date);
    if(ui_label_time) lv_obj_del(ui_label_time);
    if(ui_label_duration) lv_obj_del(ui_label_duration);
    
    // 删除定时器
    if(ui_time_update_timer) {
        lv_timer_del(ui_time_update_timer);
    }

    // 重置全局变量
    ui_label_date = NULL;
    ui_label_time = NULL;
    ui_label_duration = NULL;
    ui_time_update_timer = NULL;
    start_time = 0;
}
    